using System;
using System.Linq;
using System.Collections.Generic;

namespace Predictor
{
    class MainClass
    {
        public static void Main(string[] args)
        {
			RunDemo();
//			CleanWordnet.Clean(Predictor.wordnetPath);
//			TrieUnitTest t = new TrieUnitTest();
		}


        /// <summary>
        /// Runs a parser that reads the input and prints
        /// the predictions that made in every typed letter
        /// Author: Panos
        /// </summary>

        private static void RunDemo()
        {
            char character;
            Predictor predictor = new Predictor();
            Dictionary<char, float> predictions = new Dictionary<char, float>();
			string text = "";
			
            predictor.Load("predictor_instance.txt");
			
			Console.WriteLine("Predictor loaded");

            do
            {
                ConsoleKeyInfo key = Console.ReadKey();
                character = key.KeyChar;

                if (character == '0')
                {
                    break;
                }
                
                if (!((character >= 'a' & character <= 'z') || (character >= 'A' && character <= 'Z') || character == ' ' || character == ',' || character == '.' || character == '!'))
                {
                    /* ignore non valid input */
					
                    continue;
                }
								
                predictor.CharacterTyped(character);

                text += character;

                Console.Clear();
                Console.WriteLine(text);

                predictions = predictor.GetPredictions();

                if (predictions.Count == 0)
                {
                    Console.WriteLine("Unknown word detected!\n");
                }
				
				/* Sort predictions */

  				List<KeyValuePair<char, float>> sortedPredictions = (from kv in predictions orderby kv.Value descending select kv).ToList();
							
				foreach (KeyValuePair<char, float> prediction in sortedPredictions)
                {
					if (prediction.Value != 0)
					{
                 	   Console.WriteLine("P(" + prediction.Key + ") = " + prediction.Value);
                	}
				}

            } while (true);
			
			predictor.Save("predictor_instance.txt");
        }	
	}
}

